package com.dayuanit.dy16.atm.controller;

import com.alibaba.excel.EasyExcel;
import com.dayuanit.dy16.atm.dto.FlowDTO;
import com.dayuanit.dy16.atm.dto.ResponseDTO;
import com.dayuanit.dy16.atm.service.IFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class FlowController extends BaseController {

    @Autowired
    private IFlowService flowService;

    @RequestMapping("/flow/queryFlows")
    public ResponseDTO queryFlows(long cardId, String password, @RequestParam(required = false, defaultValue = "1") int currentPage) {
        return ResponseDTO.success(flowService.queryFlows(cardId, password, currentPage));
    }

    @RequestMapping("/flow/queryTop10Flow")
    public ResponseDTO queryTop10Flow() {
        return ResponseDTO.success(flowService.queryTopFlow3(getUserId()));
    }

    @RequestMapping("/flow/downLoadFlow")
    public void downLoadFlow(long cardId, String password, HttpServletResponse response) {
        List<FlowDTO> flowDTOS = flowService.downLoadFlow(cardId, password, getUserId());
        flowDTOS.forEach(System.out::println);

        String tableTitle = "卡号,金额,备注,时间";
        try {
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(flowDTOS.get(0).getCardNumber(),"UTF-8") + ".csv");

            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(tableTitle.getBytes("utf-8"));
            outputStream.write("\n".getBytes("utf-8"));

            for (FlowDTO flow : flowDTOS) {
                String flowValue = flow.getCardNumber() + "," + flow.getAmount() + "," + flow.getRemark() + "," + flow.getCreateTime();
                outputStream.write(flowValue.getBytes("utf-8"));
                outputStream.write("\n".getBytes("utf-8"));
                outputStream.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/flow/downLoadFlowExcel")
    public void downLoadFlowExcel(long cardId, String password, HttpServletResponse response) {
        List<FlowDTO> flowDTOS = flowService.downLoadFlow(cardId, password, getUserId());
        flowDTOS.forEach(System.out::println);

        String tableTitle = "卡号,金额,备注,时间";
        try {
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(flowDTOS.get(0).getCardNumber(),"UTF-8") + ".xlsx");

            ServletOutputStream outputStream = response.getOutputStream();

            EasyExcel.write(response.getOutputStream(), FlowDTO.class).sheet("流水").doWrite(flowDTOS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
